getwd()Session 4
Navigating through the menus in RStudio is easy, (click and go) but requires using the menu every time the user runs the code.
Go to Session -> Set Working Directory. Two convenient options are:
Choose Directory…: Choose the directory yourself
To Source File Location: Set the working directory to the directory where your R Script (the source file) is saved
R interacts with files in several ways.
Datasets can come in different formats.
NAME MONTH TEMP
99091 EINDHOVEN 1 10.6
99122 EINDHOVEN 2 7.1
99151 EINDHOVEN 3 10.2
99178 EINDHOVEN 4 8.9
99207 EINDHOVEN 5 18.5
99238 EINDHOVEN 6 15.0
99268 EINDHOVEN 7 15.0
99299 EINDHOVEN 8 20.7
99329 EINDHOVEN 9 22.6
99359 EINDHOVEN 10 10.0
99390 EINDHOVEN 11 10.5
99415 EINDHOVEN 12 9.7
99801 MAASTRICHT 1 9.7
99832 MAASTRICHT 2 5.9
99861 MAASTRICHT 3 9.9
99888 MAASTRICHT 4 9.0
99917 MAASTRICHT 5 15.7
99948 MAASTRICHT 6 14.1
99978 MAASTRICHT 7 14.9
100009 MAASTRICHT 8 20.5
100039 MAASTRICHT 9 21.6
100069 MAASTRICHT 10 10.3
100100 MAASTRICHT 11 10.2
100125 MAASTRICHT 12 9.2
Option 1: Using menus within RStudio is the easiest (click and go) but requires using the menu every time the user runs the code.
Option 1: Using menus within RStudio (cont’d)
Option 1: Using menus within RStudio (cont’d)
Option 1: Using menus within RStudio (cont’d)
Advice for option 1:
Advice for option 1 (cont’d):
save() saves objects as an .RData file.save.image() saves a selection of objects as an .RData file.save.image().save().long_data, and save this list using function save. To create plots with R’s standard graphics package, there are high-level and low-level plotting functions.
Notice that function plot() calls methods.
It will perform different operations depending on the class of the passed object. (We study the lm() function in detail in the next session!)
The plot() function takes several many arguments that can change the layout of the plots. See ?par for all graphical options; there are many!
Some examples:
col: color of lines / pointslty, lwd: Line type and thicknesspch: Point type (1-16)main, sub: Title, subtitlexlab, ylab: x and y axis labelslog, xlog and ylog for logarithmic scalesxlim, ylim: x and y axis limits (for overriding R’s default choices)mfcol, mfrow: Multiple plots in one graphics window (column-wise/row-wise)lines: Draw linesabline: Quickly add horizontal, vertical lines, and lines using equation \(y = bx + a\)points: Add pointsarrows: Add arrowstitle: Add a titlelegend: Add a legendtext: Add text at \((x,y)\) coordinatesmtext: Add text with positional specification like side=1,...,4We want to visualize the daily maximum temperatures in the climate data specifically for Maastricht. First, make a basic plot of temperatures in Maastricht then customise the plot in the following ways:
The title of the X-axis should say ‘Month’, the title of the Y-axis ‘Average Temperature’.
Make the plot a line plot with a blue line. (Hint: specifying the colour literally as "blue" works)
Make the tick marks appear on the inside of the figure rather than the outside.
Calculate the average temperature.
Add a horizontal line with the average maximum temperature
You will need to consult the help file for this exercise; see this therefrom more as an exercise in how to navigate R’s help system, than an exercise in plotting (which we will cover in more detail later).
You may want to ask ChatGPT for help and then try to see if you could also have gotten the same answer yourself; it may not always give you the most straightforward answer though!
You can manually save graphs of several formats.
Best practice is to save a graph through a device such as pdf or similar:
pdf(): Adobe PDF (easily integrated into LaTeX).svg(): Scalable Vector Graphics (commonly used on websites).png(), jpeg(), tiff(), bmp(): Various bitmap formats.